﻿using System;
using UnityEngine;
using UnityEngine.SceneManagement;
using Object = System.Object;

namespace Framework.Resource
{

    public class EditorResourceHelper : IResourceHelper
    {
        public void InitResource(InitResourceCallback callback)
        {
            callback?.Invoke(true);

        }

        public void UpdateResourceVersion(UpdateResourceVersionCallback versionCallback)
        {
            versionCallback?.Invoke(true,false);

        }

        public void LoadAsset<T>(string assetName, LoadAssetCallback callback ,object userData) where T : UnityEngine.Object
        {
#if UNITY_EDITOR
           var asset = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(assetName);
           callback?.LoadAssetSuccessCallback.Invoke(assetName,asset,userData);
#endif  
        }

        public void LoadAsset(string assetName, Type assetType, LoadAssetCallback callback , object userData)
        {
#if UNITY_EDITOR
            Object asset;
            if (assetType != null)
            {
                 asset = UnityEditor.AssetDatabase.LoadAssetAtPath(assetName, assetType);
            }
            else
            {
                  asset = UnityEditor.AssetDatabase.LoadMainAssetAtPath(assetName);
            }
            callback?.LoadAssetSuccessCallback?.Invoke(assetName,asset,userData);
#endif         
        }

        public void UnloadAsset(string assetName, UnloadAssetCallback callback)
        {
            callback?.Invoke(assetName,true);
        }

        public void UnloadAsset(object asset, UnloadAssetCallback callback)
        {
            callback?.Invoke("assetName",true);
        }


        public void LoadScene(string sceneAssetName, LoadSceneCallback callback, LoadSceneMode mode, bool suspendLoad)
        {
            AsyncOperation asyncOperation =UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(sceneAssetName, LoadSceneMode.Additive);
            if(asyncOperation==null) return;
            asyncOperation.completed += (operation) =>
            {
                if(callback==null) return;
                if (operation.allowSceneActivation)
                {
                    callback(sceneAssetName,true);
                }
                else
                {
                    callback(sceneAssetName,false);
                }
            };
        }

        public void UnLoadScene(string sceneAssetName, UnloadSceneCallback callback)
        {
            AsyncOperation asyncOperation = UnityEngine.SceneManagement.SceneManager.UnloadSceneAsync(sceneAssetName);
            if (asyncOperation == null)
            {
                return;
            }
            asyncOperation.completed += (operation) =>
            {
                if(callback==null) return;
                
                if (operation.isDone)
                {
                    callback?.Invoke(sceneAssetName, true);
                }
                else
                {
                    callback?.Invoke(sceneAssetName, false);
                }
            };
        }

        public void UnLoadUnusedAssets(UnLoadUnusedAssetsCallback callback)
        {
            callback?.Invoke(true);
        }

        public T LoadAssetSync<T>(string assetName) where T : UnityEngine.Object
        {
#if UNITY_EDITOR
            Object asset;
            {
                  asset = UnityEditor.AssetDatabase.LoadMainAssetAtPath(assetName);
            }
            return asset as T;
#endif
            throw new GameFrameworkException("暂不支持");
        }

        public object LoadAssetSync(string assetName, Type assetType)
        {
#if UNITY_EDITOR
            Object asset;
            {
                  asset = UnityEditor.AssetDatabase.LoadMainAssetAtPath(assetName);
            }
            return asset;
#endif
            throw new GameFrameworkException("暂不支持");
        }

        public void DownRemoteResource(DownloadCallback downloadCallback)
        {
            throw new NotImplementedException();
        }
    }

}